!function() {
    layui.form();
    var b = layui.jquery,
        d = layui.layer,
        l = layui.laytpl;
    b("#loading").hide().remove();
    var c = {
        // reload: function(a) {
        //     c.hidemenu();
        //     window.location.reload();
        // },
        users:function (a) {
            b(".opening-menu").removeClass("opening-menu-on");
        },
        loginout: function(a) {
            c.hidemenu();
            b(".opening-menu").removeClass("opening-menu-on");
            layer.confirm('确定要注销当前用户退出系统吗？', {
                btn: ['确定','取消'] //按钮
            }, function(){
                var param = {url:'/login/out',type:'GET'};
                var request = b.ajax(param);
                request.done(function(d){
                    if(d.error == 0){
                        window.location.href = '/login';
                    }
                }).fail(function () {
                    tips("网络异常");
                });
            }, function(){

            });
            //layer.alert("注销登录")
        },
        closeall: function(a) {
            a = b(".taskbar-app").length;
            c.hidemenu();
            1 > a || d.alert("确定关闭所有窗口？", {
                icon: 0,
                btn: ["确定", "取消"],
                zIndex: parseInt(d.zIndex + 1),
                yes: function(a, e) {
                    b(document).find(".taskbar-app").remove();
                    d.closeAll("iframe");
                    d.close(a)
                },
                end: function() {}
            })
        },
        showdesktop: function(a) {
            c.hidemenu();
            b(document).find(".layui-layer .layui-layer-min").click();
            b(document).find(".taskbar-app").removeClass("taskbar-app-on");
        },
        hidemenu: function(a) {
            b(".desktop-menu").hide()
        },
        hidopeningemenu: function() {
            //b(".opening-menu").removeClass("opening-menu-on")
        },
        openingmenu: function(a) {
            b("#opening-menu").toggleClass("opening-menu-on").off("mousedown", c.stope).on("mousedown", c.stope);
            b(document).off("mousedown", c.hidopeningemenu).on("mousedown", c.hidopeningemenu);
            b(window).off("resize", c.hidopeningemenu).on("resize", c.hidopeningemenu);
            a.off("mousedown", c.stope).on("mousedown", c.stope)
        },
        hide: function() {
            d.closeAll("tips");
        },
        pattern: function(a) {
            var b = new Date,
                e = {
                    "M+": b.getMonth() + 1,
                    "d+": b.getDate(),
                    "h+": 0 == b.getHours() % 12 ? 12 : b.getHours() % 12,
                    "H+": b.getHours(),
                    "m+": b.getMinutes(),
                    "s+": b.getSeconds(),
                    "q+": Math.floor((b.getMonth() + 3) / 3),
                    S: b.getMilliseconds()
                },
                c = {
                    0: "日",
                    1: "一",
                    2: "二",
                    3: "三",
                    4: "四",
                    5: "五",
                    6: "六"
                };
            /(y+)/.test(a) && (a = a.replace(RegExp.$1, (b.getFullYear() + "").substr(4 - RegExp.$1.length)));
            /(E+)/.test(a) && (a = a.replace(RegExp.$1, (1 < RegExp.$1.length ? 2 < RegExp.$1.length ? "星期" : "周" : "") + c[b.getDay() + ""]));
            for (var d in e)(new RegExp("(" + d + ")")).test(a) && (a = a.replace(RegExp.$1, 1 == RegExp.$1.length ? e[d] : ("00" + e[d]).substr(("" + e[d]).length)));
            return a;
        },
        refreshtime: function() {
            b(".taskbar-time").attr("title", c.pattern("yyyy年MM月dd日 EEE"));
            b("#laydate-hs").text(c.pattern("HH:mm"));
            b("#laydate-ymd").text(c.pattern("yyyy/MM/dd"));
        },
        appopen: function(a) {
            var c = !0,
                e = a.data();
            b(document).find(".taskbar-app").each(function(d, f) {
                b(f).attr("title") == e.title && (a.removeClass("disabled"), b(f).click(), c = !1)
            });
            if (c) {
                var f = b(".taskbar-app").length + 1,
                    h = parseInt((layui.jquery(".desktop-taskbar").width() - 160) / 110);
                if (f > h) d.alert("请先关闭一些窗口！", {
                    title: "官人休息下？",
                    icon: 2,
                    zIndex: d.zIndex + 1
                }, function(b) {
                    a.removeClass("disabled");
                    d.close(b)
                });
                else {
                    var f = e.width ? e.width : .8 * b(".desktop-container").width(),
                        h = e.height ? e.height : .9 * b(".desktop-container").height(),
                        g = "",
                        k = d.open({
                            type: 2,
                            title: [e.title, "background-color:#c9c9c9;color:#fff"],
                            shadeClose: !0,
                            shade: !1,
                            area: [f + "px", h + "px"],
                            content: e.url,
                            maxmin: true,
                            zIndex: d.zIndex,
                            skin:'layui-layer-molv', //layui-layer-molv,layui-layer-lan
                            //skin: "desktop-win-app",
                            success: function(b, c) {
                                a.removeClass("disabled");
                                d.setTop(b);
                                b.find(".layui-refreswind").is(":visible") || b.find(".layui-layer-setwin").prepend('<a class="layui-icon small-click layui-refreswind" data-type="refreshWind" data-id="' + c + '">&#x1002;</a>')
                            },
                            min: function(a, c) {
                                b(a).hide();
                                b("#" + g).removeClass("taskbar-app-on");
                                var e = [];
                                b(document).find(".layui-layer-iframe:visible").each(function(a, c) {
                                    e.push(b(c).css("z-index"))
                                });
                                if (1 > e.length) return !1;
                                var d = e.sort().pop();
                                b(document).find(".layui-layer-iframe:visible").each(function(a, c) {
                                    if (b(c).css("z-index") == d) return b("#taskbar-" + b(c).attr("id")).addClass("taskbar-app-on"), !1
                                });
                                return !1
                            },
                            full: function(b, a) {

                            },
                            restore: function(b, a) {},
                            moveEnd: function() {
                                b("#" + g).addClass("taskbar-app-on").siblings().removeClass("taskbar-app-on")
                            },
                            cancel: function(a) {
                                var c = layui.data("desktop-app")["desktop-app-" + a];
                                layui.each(c, function(b, a) {
                                    d.close(a)
                                });
                                layui.data("desktop-app", {
                                    key: "desktop-app-" + a,
                                    remove: !0
                                });
                                b("#" + g).remove()
                            },
                            end: function() {
                                a.removeClass("disabled")
                            }
                        }),
                        g = "taskbar-layui-layer" + k,
                        f = "",
                        f = e.isicon ? "" + ('<div class="layui-inline layui-elip taskbar-app taskbar-app-on" title="' + e.title + '" id="' + g + '"><i class="layui-icon" style=" background-color:' + e.iconbg + '">' + e.icon + '</i><span class="desktop-title layui-elip">' + e.title + "</span></div>") : "" + ('<div class="layui-inline layui-elip taskbar-app taskbar-app-on" title="' + e.title + '" id="' + g + '"><span class="desktop-title layui-elip">' + e.title + "</span></div>");
                    b("#" + g).is(":visible") || (b(".desktop-taskbar-app-list").append(f), b("#" + g).on("click", function() {
                        var a = b(this);
                        a.hasClass("taskbar-app-on") ? b("#layui-layer" + k).find(".layui-layer-setwin .layui-layer-min").click() : (a.addClass("taskbar-app-on").siblings().removeClass("taskbar-app-on"), b("#layui-layer" + k).show(), d.zIndex = parseInt(d.zIndex + 1), d.style(k, {
                                zIndex: d.zIndex
                            }))
                    }).siblings().removeClass("taskbar-app-on"))
                }
            }
        },
        stope: function(e) {
            a = a || window.event;
            a.stopPropagation ? a.stopPropagation() : a.cancelBubble = !0
        },
        arrange: function(c) {
            a = b(".swiper-slide-active").index();
            a = b(".desktopContainer:eq(" + ("" == a || void 0 == a ? 0 : a) + ")");
            var c = b(".desktopContainer"),
                e = 0,
                d = 0,
                h = 96,
                g = 96,
                k = 0,
                k = c.height() - 40;
            c.width();
            a.find(".desktop-app").each(function(a, c) {
                b(c).css("top", d + "px");
                b(c).css("left", e + "px");
                g = b(c).height();
                h = b(c).width();
                d = d + g + 10 + 10;
                d >= k - 65 && (d = 0, e = e + h + 10)
            })
        },
        refreshWind: function(a) {
            a = a.data("id");
            url = b("#layui-layer-iframe" + a).attr("src");
            d.iframeSrc(a, url)
        },
        init: function() {

            var param = {url:'/desktop/menu'};
            var request = b.ajaxs(param);
            request.done(function (data) {
                var obj = data.data;
                var apps = obj.apps;
                var menu = obj.menu;
                apps = eval("("+apps+")");
                menu = eval("("+menu+")");
                obj.apps = apps;
                obj.menu = menu;
                console.log(obj)
                var desktpData = obj;
                l('{{# layui.each(d.menu, function(index, menuitem){ if(index>=3)return false;}}<div class="swiper-slide"><div class="desktopContainer"  data-menuid="{{menuitem.menuid}}" data-name="{{menuitem.name}}" >{{# layui.each(menuitem.app, function(index, app){}}<div class="desktop-app" data-id="{{d.apps[app].appid}} " data-title="{{d.apps[app].name}}" data-url="{{d.apps[app].url}}" data-icon="{{d.apps[app].icon}}" data-iconbg="{{d.apps[app].iconbg}}"  data-isicon="{{d.apps[app].isicon}}" data-height="{{d.apps[app].height}}" data-width="{{d.apps[app].width}}" data-fid="{{app}}"><i class="layui-icon" style="background-color:{{d.apps[app].iconbg}}">{{d.apps[app].icon}}</i><span class="desktop-title layui-elip">{{d.apps[app].name}}</span></div>{{# });}}</div></div>{{# }); }} ').render(desktpData, function(a) {
                    b(".swiper-wrapper").html(a)
                });
                l('{{# layui.each(d.menu[3].app, function(index, app){}}<div class="desktop-app" data-id="{{d.apps[app].appid}} " data-title="{{d.apps[app].name}}" data-url="{{d.apps[app].url}}" data-icon="{{d.apps[app].icon}}" data-iconbg="{{d.apps[app].iconbg}}"  data-isicon="{{d.apps[app].isicon}}" data-height="{{d.apps[app].height}}" data-width="{{d.apps[app].width}}" data-fid="{{app}}"><i class="layui-icon" style="background-color:{{d.apps[app].iconbg}}">{{d.apps[app].icon}}</i><span class="desktop-title layui-elip">{{d.apps[app].name}}</span></div>{{# });}}').render(desktpData, function(a) {
                    b(".opening-menu-app-list").html(a)
                });
                b(".desktop-container").css("height", b(window).height() - 30);
                new Swiper(".swiper-container", {
                    pagination: ".swiper-pagination",
                    simulateTouch: !1,
                    slidesPerView: 1,
                    paginationClickable: !0,
                    spaceBetween: 30,
                    keyboardControl: !0,
                    mousewheelControl: !0,
                    onSlideChangeEnd: function(a) {
                        c.arrange(a.realIndex)
                    }
                });
                b(window).resize(function(a) {
                    b(".desktop-container").css("height", b(window).height() - 40);
                    b(".desktopContainer").css("height", b(".desktop-container").height());
                    c.arrange()
                });
                b(".desktopContainer").sortable({
                    revert: !0
                });
                b(".desktopContainer").sortable({
                    connectToSortable: ".desktopContainer",
                    stop: function(a, b) {
                        c.arrange()
                    }
                }).disableSelection();
                c.arrange();
                setInterval(c.refreshtime, 1E3);
                b(document).contextmenu(function() {
                    return !1
                });
                b(".desktopContainer").on("contextmenu", function(a) {
                    var c = a.clientX;
                    a = a.clientY;
                    var d = b(".desktop-menu"),
                        f = document.body.clientWidth,
                        h = document.body.clientHeight,
                        c = c + d.width() >= f ? f - d.width() - 15 : c;
                    a = a + d.height() >= h - 40 ? h - d.height() - 15 : a;
                    d.css({
                        top: a,
                        left: c
                    }).show()
                });
                b(".desktop-app").on("click", function() {
                    var a = b(this);
                    if (a.hasClass("disabled")) return !1;
                    a.addClass("disabled");
                    c.appopen(a)
                })
            })
        }
    };
    c.init();
    b("body").on("click", ".small-click", function() {
        var a = b(this),
            d = a.data("type");

        c[d] ? c[d].call(this, a) : ""
    })
}();